const { Op } = require("sequelize");
var COS = require('cos-nodejs-sdk-v5');

const artPaintingModel = appRequire('models/artPainting');
const collectModel = appRequire('models/collect');

// 导出对象，供其它模块调用
module.exports = {
    list,
    save,
    destroy,
    detail,
};

async function list(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, [], request);
    if (checkResult == false) return;

    let params = {}
    let orders = [['createdAt', 'DESC']]

    if (postData.isNice) params.isNice = postData.isNice
    if (postData.order) {
        if (postData.order.indexOf("-") == -1) {
            orders = [[postData.order, 'ASC']]
        } else {
            orders = [[postData.order.split("-")[1], 'DESC']]
        }
    }
    if (postData.searchText) {
        params[Op.or] = [
            { title: { [Op.like]: '%' + postData.searchText + '%' } },
            { desc: { [Op.like]: '%' + postData.searchText + '%' } },
        ]
    }
    if (postData.contents) params.nameZh = postData.contents

    let headerParams = {
        where: params,
        order: orders,
    }

    if (postData.pageSize) {
        headerParams.limit = postData.pageSize
        headerParams.offset = postData.pageSize * (postData.pageNum - 1)
    }
    let paint = await artPaintingModel.findAndCountAll(headerParams)
    paint = Common.clone(paint)

    for (let item of paint.rows) {
        item.imageUrl = item.imageUrl + "?imageMogr2/format/webp/thumbnail/!10p"
    }

    returnData.maxCount = paint.count
    returnData.maxPage = Math.ceil(paint.count / postData.pageSize)
    returnData.data = paint.rows
    request.json(returnData)
}

//画作详情
async function detail(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['objectId'], request);
    if (checkResult == false) return;

    let paint = await artPaintingModel.findOne({
        where: {
            objectId: postData.objectId
        },
        //attributes: ["objectId", "number", "price", "title", "desc", "content", "downLoadNum", "classDesc", "tags", "images", "videoUrl", "createdAt"]
    })
    paint = Common.clone(paint)
    paint.collect = 0
    if (postData.uid) { //登陆获取是否收藏
        result2 = await collectModel.collect.findOne({
            where: {
                artpptId: paint.objectId,
                userId: postData.uid,
            }
        })
        if (result2) paint.collect = 1
    }

    returnData.data = paint
    request.json(returnData)
}

async function save(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['nameZh', 'images'], request);
    if (checkResult == false) return;

    let params = {
        images: postData.images,
        nameZh: postData.nameZh.split("\n")[0],
        type: postData.type,

        price: postData.price,
        number: postData.number,
        isNice: postData.isNice,
    }

    if (postData.objectId) {
        let result = await artPaintingModel.update(params, {
            where: {
                objectId: postData.objectId
            }
        })
        returnData.data = result
    } else {

        /*let thisResult = await artPaintingModel.findOne({
            where: {
                nameZh: postData.nameZh.split("\n")[0],
            }
        })

        if (thisResult) {
            returnData.code = 0
            returnData.msg = "已存在此画作"
            return request.json(returnData)
        }*/

        params.objectId = sid.uuid()
        let result = await artPaintingModel.create(params)
        returnData.data = result
    }

    request.json(returnData)
}

async function destroy(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['objectId'], request);
    if (checkResult == false) return;

    let result = await artPaintingModel.destroy({
        where: {
            objectId: postData.objectId
        }
    })
    returnData.data = result
    request.json(returnData)
}